package com.xzg.erp.product.web;

import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xzg.erp.common.BusinessException;
import com.xzg.erp.common.Constants;
import com.xzg.erp.product.config.ProductDataListener;
import com.xzg.erp.product.entity.Product;
import com.xzg.erp.product.entity.dto.ProductDto;
import com.xzg.erp.product.service.IProductService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 产品表 前端控制器
 * </p>
 *
 * @author qwc
 * @since 2025-02-12
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;

    @PostMapping("/add")
    public ResponseEntity addTag(@Valid @RequestBody ProductDto dto) {
        Product one = productService.getOne(Wrappers.<Product>lambdaQuery().eq(Product::getCode, dto.getCode()).eq(Product::getDeleted, Constants.UN_DELETED));
        if (Objects.nonNull(one)) {
            throw new BusinessException(HttpStatus.CONFLICT, Constants.DATA_EXIT);
        }
        productService.saveProduct(dto);
        return ResponseEntity.ok(Constants.SUCCESS);
    }

    @PostMapping("/page")
    public ResponseEntity page(@RequestBody ProductDto dto) {
        Page<Product> page = new Page<>(dto.getPageNo(), dto.getPageSize());
        Page<Product> page1 = productService.page(page,Wrappers.<Product>lambdaQuery()
                        .like(StrUtil.isNotBlank(dto.getCode()),Product::getCode,dto.getCode())
                        .like(StrUtil.isNotBlank(dto.getName()),Product::getName,dto.getName())
                .eq(Product::getDeleted, Constants.UN_DELETED)
                .orderByDesc(Product::getUpdatedAt));
        return ResponseEntity.ok(page1);
    }

    @PostMapping("/delete/{ids}")
    public ResponseEntity delete(@PathVariable List<Integer> ids) {
        productService.logicDeleteByIds(ids);
        return ResponseEntity.ok(Constants.SUCCESS);
    }

    @PostMapping("/update")
    public ResponseEntity update(@RequestBody ProductDto dto) {
        productService.updateProduct(dto);
        return ResponseEntity.ok(Constants.SUCCESS);
    }

    @GetMapping("/getById/{id}")
    public ResponseEntity<Product> getById(@PathVariable Integer id) {
        Product product = productService.getOne(Wrappers.<Product>lambdaQuery().eq(Product::getId, id).eq(Product::getDeleted,Constants.UN_DELETED));
        return ResponseEntity.ok(product);
    }

    @PostMapping("/import")
    public ResponseEntity importExcel(@RequestParam("file") MultipartFile file) throws IOException {
        ProductDataListener productDataListener = new ProductDataListener(productService);
        EasyExcel.read(file.getInputStream(), ProductDto.class, productDataListener)
                .sheet()
                .doRead();
        return ResponseEntity.ok(productDataListener.getResult());
    }


}
